package com.hw.ui.upload_data;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.hw.R;
import com.hw.bean.AbRelationBean;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.ButterKnife;

public class RelationAdapter extends BaseAdapter {
    private Context mContext;
    LayoutInflater inflater;
    List<AbRelationBean> mlists = new ArrayList<AbRelationBean>();
    private int selectedPosition = -1;// 选中

    public void setSelectedPosition(int position) {
        selectedPosition = position;
    }

    public RelationAdapter(Context context, List<AbRelationBean> lists) {
        this.mContext = context;
        inflater = LayoutInflater.from(context);
        mlists = lists;
    }

    @Override
    public int getCount() {
        return mlists == null ? 0 : mlists.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_relation, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv2.setText(mlists.get(position).getPname());
        viewHolder.tv3.setText(mlists.get(position).getBatch());
        viewHolder.tv4.setText(mlists.get(position).getCasec()+"");
        if(selectedPosition == position)
        {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.itemabove);
            drawable.setBounds(0,0,50,50);
            viewHolder.tv4.setCompoundDrawables(null,null,drawable,null);
            viewHolder.tv4.setCompoundDrawablePadding(10);
            viewHolder.idFlowlayout.setVisibility(View.VISIBLE);
            String[] mVals = new String[mlists.get(position).getBoxs().size()];
            for (int i = 0; i < mlists.get(position).getBoxs().size(); i++) {
                mVals[i] = mlists.get(position).getBoxs().get(i);
            }
            final TagFlowLayout tagFlowLayout = viewHolder.idFlowlayout;
            viewHolder.idFlowlayout.setAdapter(new TagAdapter<String>(mVals) {
                @Override
                public View getView(FlowLayout parent, int position, String s) {
                    TextView tv = (TextView) LayoutInflater.from(mContext).inflate(R.layout.tv, tagFlowLayout, false);
                    tv.setText(s);
                    return tv;
                }
            });
        }
        else
        {
            Drawable drawable = mContext.getResources().getDrawable(R.mipmap.itembelow);
            drawable.setBounds(0,0,50,50);
            viewHolder.tv4.setCompoundDrawables(null,null,drawable,null);
            viewHolder.tv4.setCompoundDrawablePadding(10);
            viewHolder.idFlowlayout.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.tv2)
        TextView tv2;
        @Bind(R.id.tv3)
        TextView tv3;
        @Bind(R.id.tv4)
        TextView tv4;
        @Bind(R.id.ll1)
        LinearLayout ll1;
        @Bind(R.id.id_flowlayout)
        TagFlowLayout idFlowlayout;
        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}

